<?php

declare(strict_types=1);

namespace app\common\model;

use Symfony\Component\VarDumper\Cloner\Data;
use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Db;

class GmTclpanellc extends Model
{
    use SoftDelete;
    protected $deleteTime = "delete_time";
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        //按轮次编号查找
        if ($luncinum = input("luncinum")) {
            $where[] = ["luncinum", "like", "%" . $luncinum . "%"];
        }
        //按轮次类型(1.基础局 2.普通局 3.高端局 4.老板局 5.土豪局)查找
        if ($luncitype = input("luncitype")) {
            $where[] = ["luncitype", "like", "%" . $luncitype . "%"];
        }
        //按轮次奖励查找
        if ($luncirewad = input("luncirewad")) {
            $where[] = ["luncirewad", "like", "%" . $luncirewad . "%"];
        }
        //按轮次状态(1.未开始 2.进行中 3.已结束)查找
        if ($luncistatus = input("luncistatus")) {
            $where[] = ["luncistatus", "like", "%" . $luncistatus . "%"];
        }
        $list = self::order('id', 'desc')->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }


    //区别轮次类型列表
    public static function getListWhere($type)
    {
        $where = [];
        $limit = input('get.limit');
        //按轮次类型(1.基础局 2.普通局 3.高端局 4.老板局 5.土豪局)查找
        $where[] = ["luncitype", "=",  $type];

        $list = self::order('id', 'desc')->where($where)->paginate($limit);
        return $list->items();
    }

    public static function getPlayerRmb($account, $qufu)
    {
        $serverName = self::getDBName($qufu);
        return Db::query("select rmb from  " . $serverName . ".globaluser where account='$account';");
    }

    public static function getPlayer6yk($actorid, $qufu)
    {
        $serverName = self::getDBName($qufu);
        return Db::query("select count from  " . $serverName . ".items where actorid='$actorid' and id='340033';");
    }

    public static function getmylogls($account, $actorid, $qufu, $type)
    {
        return Db::query("select b.luncinum as luncinum,b.luncistatus as luncistatus,a.istc as istc from gm_tclpaneltzb a left join gm_tclpanellc b on a.lcid=b.id"
            . " where a.cyaccount='$account' and a.cyactorid='$actorid' and a.cyqufuid='$qufu' and b.luncitype='$type' order by istc desc, luncinum desc;");
    }

    public static function getalllogls($type)
    {
        return Db::query("select a.cyqufuid as cyqufu,b.luncinum as luncinum,a.cyaccount as cyaccount,b.luncistatus as luncistatus,a.istc as istc from gm_tclpaneltzb a left join gm_tclpanellc b on a.lcid=b.id where b.luncitype='$type' order by istc desc, luncinum desc;");
    }

    public static function getCanyu($account, $actorid, $qufu, $lunciid, $luncitype)
    {
        $sql2 = "";
        $serverName = self::getDBName($qufu);
        $luncimoney="0";
        
        if ($luncitype == "1") {
        } else if ($luncitype == "2") {
            //查询6元卡是否足够
            // $row = Db::query("select count from  " . $serverName . ".items where actorid='$actorid' and id='340033';");
            // $oldcount=$row[0]["count"];

            // $luncimoney = 50000;//需要扣减的数量
            // if (intval($oldcount) < $luncimoney) {return null;}
            // $sql2 = "INSERT INTO $serverName.gmcmd (serverid,cmdid,cmd,param1,param2,param3,param4,param5) 
            //             VALUES('$qufu','1','gmadditem','$actorid','340033','-$luncimoney','','')";
            
            // $sql4="update $serverName.items set count=count-$luncimoney where  actorid='$actorid' and id='340033';";

            // $sql = "insert into gm_tclpaneltzb(lcid,cyaccount,cyactorid,cyqufu,cyqufuid,istc,create_time)" .
            // "values('$lunciid','$account','$actorid','','$qufu','1','" . date('Y-m-d H:i:s') . "')";
            // $sql1 = "update gm_tclpanellc set currentprenum=currentprenum+1 where id='$lunciid';";

            // $nowrmb=intval($oldcount)-intval($luncimoney);
            // $sql3 = "INSERT INTO gm_conslog (`serverid`, `qufu`, `account`, `oldrmb`, `nowrmb`, `consrmb`, `itemid`, `xftime`) 
            // VALUES ('$lunciid', '$serverName', '$account', '$oldcount', '$oldcount', '-$luncimoney', '参与头筹(6元卡)', NOW());";
            // try {
            //     Db::execute($sql);
            //     Db::execute($sql1);
            //     Db::execute($sql2);
            //     Db::execute($sql3);
            //     Db::execute($sql4);
            // } catch (\Throwable $th) {
            //     throw $th;
            // }
            
            //查询余额是否足够
            $row = Db::query("select rmb from " . $serverName . ".globaluser where account='$account';");
            $oldrmb=$row[0]["rmb"];

            $luncimoney = 1000000;
            if (intval($oldrmb) < $luncimoney) {return null;}
            $sql2 = "update " . $serverName . ".globaluser set rmb=rmb-$luncimoney where account='$account';";

            $sql = "insert into gm_tclpaneltzb(lcid,cyaccount,cyactorid,cyqufu,cyqufuid,istc,create_time)" .
            "values('$lunciid','$account','$actorid','','$qufu','1','" . date('Y-m-d H:i:s') . "')";
            $sql1 = "update gm_tclpanellc set currentprenum=currentprenum+1 where id='$lunciid';";

            $nowrmb=intval($oldrmb)-intval($luncimoney);
            $sql3 = "INSERT INTO gm_conslog (`serverid`, `qufu`, `account`, `oldrmb`, `nowrmb`, `consrmb`, `itemid`, `xftime`) 
            VALUES ('$lunciid', '$serverName', '$account', '$oldrmb', '$nowrmb', '-$luncimoney', '参与头筹', NOW());";
            Db::execute($sql);
            
            Db::execute($sql1);
            
            Db::execute($sql2);

            Db::execute($sql3);


        } else if ($luncitype == "3") {
            //查询余额是否足够
            $row = Db::query("select rmb from " . $serverName . ".globaluser where account='$account';");
            $oldrmb=$row[0]["rmb"];

            $luncimoney = 2000000;
            if (intval($oldrmb) < $luncimoney) {return null;}
            $sql2 = "update " . $serverName . ".globaluser set rmb=rmb-$luncimoney where account='$account';";

            $sql = "insert into gm_tclpaneltzb(lcid,cyaccount,cyactorid,cyqufu,cyqufuid,istc,create_time)" .
            "values('$lunciid','$account','$actorid','','$qufu','1','" . date('Y-m-d H:i:s') . "')";
            $sql1 = "update gm_tclpanellc set currentprenum=currentprenum+1 where id='$lunciid';";

            $nowrmb=intval($oldrmb)-intval($luncimoney);
            $sql3 = "INSERT INTO gm_conslog (`serverid`, `qufu`, `account`, `oldrmb`, `nowrmb`, `consrmb`, `itemid`, `xftime`) 
            VALUES ('$lunciid', '$serverName', '$account', '$oldrmb', '$nowrmb', '-$luncimoney', '参与头筹', NOW());";
            Db::execute($sql);
            
            Db::execute($sql1);
            
            Db::execute($sql2);

            Db::execute($sql3);

        } else if ($luncitype == "4") {
            //查询余额是否足够
            $row = Db::query("select rmb from " . $serverName . ".globaluser where account='$account';");
            $oldrmb=$row[0]["rmb"];

            $luncimoney = 3000000;
            if (intval($oldrmb) < $luncimoney) {return null;}
            $sql2 = "update " . $serverName . ".globaluser set rmb=rmb-$luncimoney where account='$account';";

            $sql = "insert into gm_tclpaneltzb(lcid,cyaccount,cyactorid,cyqufu,cyqufuid,istc,create_time)" .
            "values('$lunciid','$account','$actorid','','$qufu','1','" . date('Y-m-d H:i:s') . "')";
            $sql1 = "update gm_tclpanellc set currentprenum=currentprenum+1 where id='$lunciid';";

            $nowrmb=intval($oldrmb)-intval($luncimoney);
            $sql3 = "INSERT INTO gm_conslog (`serverid`, `qufu`, `account`, `oldrmb`, `nowrmb`, `consrmb`, `itemid`, `xftime`) 
            VALUES ('$lunciid', '$serverName', '$account', '$oldrmb', '$nowrmb', '-$luncimoney', '参与头筹', NOW());";
            Db::execute($sql);
            
            Db::execute($sql1);
            
            Db::execute($sql2);

            Db::execute($sql3);
        } else if ($luncitype == "5") {
            //查询余额是否足够
            $row = Db::query("select rmb from " . $serverName . ".globaluser where account='$account';");
            $oldrmb=$row[0]["rmb"];

            $luncimoney = 5000000;
            if (intval($oldrmb) < $luncimoney) {return null;}
            $sql2 = "update " . $serverName . ".globaluser set rmb=rmb-$luncimoney where account='$account';";

            $sql = "insert into gm_tclpaneltzb(lcid,cyaccount,cyactorid,cyqufu,cyqufuid,istc,create_time)" .
            "values('$lunciid','$account','$actorid','','$qufu','1','" . date('Y-m-d H:i:s') . "')";
            $sql1 = "update gm_tclpanellc set currentprenum=currentprenum+1 where id='$lunciid';";

            $nowrmb=intval($oldrmb)-intval($luncimoney);
            $sql3 = "INSERT INTO gm_conslog (`serverid`, `qufu`, `account`, `oldrmb`, `nowrmb`, `consrmb`, `itemid`, `xftime`) 
            VALUES ('$lunciid', '$serverName', '$account', '$oldrmb', '$nowrmb', '-$luncimoney', '参与头筹', NOW());";
            Db::execute($sql);
            
            Db::execute($sql1);
            
            Db::execute($sql2);

            Db::execute($sql3);
        }

        
    }

    public static function updateCanyustatus($lunciid)
    {
        $sql = "update gm_tclpanellc set luncistatus=3 where id='$lunciid';";
        // return $sql;
        return Db::execute($sql);
    }

    public static function getIsCanyu($lunciid)
    {
        return Db::query("select * from gm_tclpanellc where id=:id", ['id' => $lunciid]);
    }

    public static function findmaxLunnum()
    {
        return Db::query("SELECT * FROM gm_tclpanellc where id=(SELECT MAX(id) FROM gm_tclpanellc);")[0];
    }



    // 获取数据库名称 传入$serverid
    public static function getDBName($serverid)
    {
        $newserverid = 'h5_cq_';
        // 使用正则表达式匹配并获取有效的字符
        $integerValue = intval($serverid) - 1000;
        // 如果有匹配到有效字符，则返回匹配结果，否则返回 "0"
        $trimmedString = $integerValue < 0 ? "0" : strval($integerValue);
        if (env('APP_DEBUG')) {
            $newserverid .= '00' . $trimmedString;
        } else {
            $newserverid .= '' . $trimmedString;
        }

        return $newserverid;
    }
}
